import { getLayoutProps } from "@/common/layout/getProps";
import { useRouter } from "next/navigation";
import type { Key} from "react";
import { useState,useEffect } from "react";
import { data } from "./data";
import { PageContainer } from "@ant-design/pro-layout";
import { Button, Divider, Form, Input, Modal, Row, Table, Pagination, message } from "antd";
import type { TableColumnsType , TableProps } from "antd";
import { ProForm } from "@/components/ProForm";
import { http } from "@/utils/request";

type TableDataType = {
  id: string;
  code: string;
  evaluType: string;
  field3: string;
  evaluPersion: string;
  selfScore: string;
  groupScore: string;
}

type TableRowSelection<T> = TableProps<T>["rowSelection"];

const { confirm } = Modal;
const PerformanceResultConfirm = () => {
  const router = useRouter();
  const [form] = Form.useForm()
  const [selectedRowKeys, setSelectedRowKeys] = useState<Key[]>([])
  const [list,setList] = useState();
  const [pageNum,setPageNum] = useState(1);
  const [pageSize,setPageSize] = useState(10);
  const [total,setTotal] = useState();

  const complaint = (row: TableDataType, detail = false) => {
    router.push(`/performanceResultConfirmAndApply/performanceResultConfirm/${detail ? 'complaintDetail' : 'complaintEdit'}?id=${row.id}${detail ? `&type=detail` : ''}`)
  }

  const columns: TableColumnsType<TableDataType> = [
    {
      align: "center",
      title: "序号",
      width: 60,
      key: "index",
      render: (_text, _record, index) => {
        return index + 1
      }
    },
    {
      align: "center",
      title: "单据编号",
      dataIndex: "code"
    },
    {
      align: "center",
      title: "考核类型",
      dataIndex: "evaluType",
      render: (_, record) => {
        return (
          <>
            {record.evaluType === "10" && <span>部门</span>}
            {record.evaluType === "20" && <span>员工</span>}
          </>
        )
      }

    },
    {
      align: "center",
      title: "考核对象",
      dataIndex: "objectName"
    },
    {
      align: "center",
      title: "期间",
      dataIndex: "evaluPersion"
    },
    {
      align: "center",
      title: "自评得分",
      dataIndex: "selfScore"
    },
    {
      align: "center",
      title: "考评小组评分",
      dataIndex: "groupScore"
    },
    {
      align: "center",
      title: "状态",
      dataIndex: "appealStatus",
      render: (_, record) => {
        return (
          <>
            {record.appealStatus === "0" && <span>待提交</span>}
            {record.appealStatus === "1" && <span>已申诉</span>}
            {record.appealStatus === "2" && <span>已驳回</span>}
            {record.appealStatus === "3" && <span>已确认</span>}
          </>
        )
      }
    },
    
    {
      align: "center",
      title: "操作",
      render: (_, record) => {
        return (
          <>
            {record.appealStatus === "0" && 
              <>
                <Button type={'link'} danger onClick={() => complaint(record)}>申诉</Button>
                <Button type={'link'} onClick={() => {
                  confirm({
                    title: '提示?',
                    content: '此操作将确认该条绩效结果，是否继续？',
                    onOk() {
                      confirmFun(record.id)
                    },
                  });
                }}>确认</Button>
              </>
            }
            <Button type={'link'} onClick={() => complaint(record, true)}>详情</Button>
          </>
        )
      }
    }
  ]
  
  //绩效结果确认
  const confirmFun = async(id:stirng)=>{
    try {
      const res = await http.post({
        url:'/PmEvaluResult/update',
        data:{
          id:id,
        }
      })
      if(res.code==200){
        message.success('确认成功!');
        getList();
      }else{
        message.error(res.msg);
      }
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  }
  //修改页码或分页大小
  const changePagination = async (page: number, size: number)=>{
    setPageNum(page);
    if(size!==pageSize){
      setPageNum(1);
    }
    setPageSize(size);
  }

  //查询接口
  const getList = async() =>{
    try {
      const res = await http.get({
        url:'/PmEvaluResult/qryList',
        params:{
          code:form.getFieldValue('code'),
          objectId:form.getFieldValue('objectId'),
          pageNum:pageNum,
          pageSize:pageSize,
        }
      })
      setList(res.rows);
      setTotal(res.total);
    } catch (errorInfo) {
      console.log(errorInfo);
    }
  }
  useEffect(()=>{
    getList();
  },[pageNum,pageSize])
  return (
    <PageContainer>
      <ProForm form={form} layout={'inline'} onSearch={getList} onReset={getList}>
        <Form.Item
          label={'单据编号'}
          name="code"
        >
          <Input placeholder="请输入单据编号" />
        </Form.Item>
        <Form.Item
          label={'考核部门'}
          name="objectId"
        >
          <Input />
        </Form.Item>
      </ProForm>

      <Divider className={'my-[20px]'}></Divider>

      <Table
        dataSource={list}
        columns={columns}
        rowKey={(record) => `${record.id}`}
        // rowSelection={rowSelection}
        pagination={false}
      ></Table>
      <Pagination
        className={'m-[20px] text-end'}
        current={pageNum}
        pageSize={pageSize}
        total={total}
        showSizeChanger
        showQuickJumper
        showTotal={(total) => `共 ${total} 条`}
        onChange={changePagination}
      />
    </PageContainer>
  )
}

export const getServerSideProps = getLayoutProps;
export default PerformanceResultConfirm;
